package com.homedo.core.common.util;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class CommonDataUtil {

    public static boolean validate(String commonDataStr,
                                   HashMap<String, String> commonDataMap) {
        if (StringUtils.isBlank(commonDataStr)) {
            return false;
        }
        String[] commonData = commonDataStr.split(",");
        for (String cd : commonData) {
            if (!commonDataMap.containsKey(cd)) {
                return false;
            }
        }
        return true;
    }

    public static String mappedField(String fieldStr,
                                     HashMap<String, String> commonDataMap) {
        return getNameByFields(fieldStr, commonDataMap, ",");
    }

    public static String getNameByFields(String fields,
                                         Map<String, String> map, String split) {
        if (StringUtils.isBlank(split)) {
            split = "->";
        }
        if (StringUtils.isBlank(fields)) {
            return "";
        }
        if (map == null) {
            return "";
        }
        StringBuilder fieldBuilder = new StringBuilder();
        String[] fieldAry = fields.split(",");
        for (String field : fieldAry) {
            fieldBuilder.append(map.get(field)).append(split);
        }
        return fieldBuilder.substring(0, fieldBuilder.lastIndexOf(split));
    }

}
